<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <dealBaseBondForm
          v-if="form !== undefined"
          ref="dealBaseBondForm"
          :bond-form="form"
        />
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="deal:bondProduct" />
    </div>
  </div>
</template>

<script>
import dealBaseBondForm from '@/views/deal/base/bond/dealBaseBondForm'
import { saveDealBaseBond, getDealBaseBond } from '@/api/bond/dealBondProduct/dealBondProduct.js'

export default {
  components: {
    dealBaseBondForm
  },
  data() {
    return {
      // 表单参数
      form: {},
      // 遮罩层
      loading: false,
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getDicts('deal_bond_NW').then(response => {
      this.primaryBondCategoryOptions = response.data
    })
    this.getDicts('currency').then(response => {
      this.currencyOptions = response.data
    })
    this.getDicts('deal_repayment_order').then(response => {
      this.repaymentOrderOptions = response.data
    })
    this.getDicts('deal_bond_structured_type').then(response => {
      this.structuredTypeOptions = response.data
    })
    this.getDicts('deal_bond_nature_enterprise').then(response => {
      this.natureEnterpriseOptions = response.data
    })
    this.getDicts('letter_rating').then(response => {
      this.externalRatingIssuerOptions = response.data
    })
    this.getDicts('letter_rating').then(response => {
      this.ratingOutsideGuarantorOptions = response.data
    })
  },
  methods: {
    // 初始化数据
    init(data) {
      this.loading = true
      if (data != null && data.id !== undefined) {
        getDealBaseBond(data.id).then(response => {
          this.form = response.data
          this.formatterSecondaryBondCategory()
          this.loading = false
        })
      } else {
        this.reset()
        this.loading = false
      }
    },
    formatterSecondaryBondCategory() {
      try {
        var temp = this.form.secondaryBondCategory
      } catch (e) {
        console.log(e)
      }
      this.$nextTick(() => {
        this.form.secondaryBondCategory = temp
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        dealId: undefined,
        primaryBondCategory: undefined,
        secondaryBondCategory: undefined,
        bondCode: undefined,
        bondName: undefined,
        financingSubject: undefined,
        obligor: undefined,
        countryRisk: undefined,
        industryLevelOne: undefined,
        industryLevelTwo: undefined,
        currency: undefined,
        dueDate: undefined,
        repaymentOrder: undefined,
        subjectName: undefined,
        series: undefined,
        level: undefined,
        structuredType: undefined,
        bondType: undefined,
        natureEnterprise: undefined,
        nameCreditorRight: undefined,
        fundManager: undefined,
        fundSize: undefined,
        termInvestment: undefined,
        externalRatingIssuer: undefined,
        nameGuarantor: undefined,
        ratingOutsideGuarantor: undefined,
        thirdPartyPrincipal: undefined,
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      if (this.$parent.$parent.formOpen !== undefined) {
        this.$parent.$parent.formOpen = false
      }
      // 刷新list页面
      if (this.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          saveDealBaseBond(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess(this.$t('保存成功'))
              this.closeForm()
            } else {
              this.msgError(response.msg)
            }
            this.loading = false
          })
        } else {
          this.locationError(document)
        }
      })
    }

  }

}
</script>
